package com.example.backweb.controller;


import com.example.backweb.common.constant.Constant;
import com.example.backweb.common.properties.JwtProperties;
import com.example.backweb.common.result.Result;
import com.example.backweb.common.util.JwtUtil;
import com.example.backweb.pojo.TeacherIsLogin;
import com.example.backweb.pojo.Teacher;
import com.example.backweb.service.impl.TeacherServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xu
 * @since 2023-11-24
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户操作")
public class user {
    @Autowired
    TeacherServiceImpl teacherService;
    @Autowired
    JwtProperties jwtProperties;

    //登录
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result test(@RequestBody TeacherIsLogin param) {
        //获取参数
        System.out.println(param);
        Teacher teacher=param.getTeacher();
        Boolean rememberMe=param.getRememberMe();
        //获取传来的账户密码权限
        String name=teacher.getTea_id();
        String pwd=teacher.getPassword();
        Teacher teacher1 = teacherService.login(name);
        // 比对密码
        if (DigestUtils.md5DigestAsHex(pwd.getBytes()).equals(teacher1.getPassword())){
            //创建jwt
            Map<String, Object> claims = new HashMap<>();
            claims.put(Constant.USER_ID,name);
            String token = JwtUtil.createJWT(jwtProperties.getSecretKey(), jwtProperties.getJWtTime(), claims);
            return Result.success(Constant.LOGIN_SUCCESS,token);
        }
        return Result.fail(Constant.PASSWORD_ERROR);
    }
    //注册
    @PostMapping("/enroll")
    @ApiOperation("注册")
    public Result enroll(@RequestBody Teacher teacher) {
        String id=teacher.getTea_id();
        if(teacherService.isexists(id)) return Result.fail("用户已存在");
        String pwd=teacher.getPassword();
        String ls=teacher.getLast_idcard();
        if(pwd==null) pwd=ls;
        // 密码加密
        String pwdLock = DigestUtils.md5DigestAsHex(pwd.getBytes());
        teacher.setPassword(pwdLock);
        if(teacherService.enroll(teacher)) return Result.success("注册成功");
        else return Result.fail("未知错误");
    }
    //忘记密码
    @PostMapping("/forget")
    @ApiOperation("忘记密码")
    public Result forget(@RequestBody Teacher teacher) {
        String pwd=teacher.getPassword();
        String ls=teacher.getLast_idcard();
        // 新密码进行加密处理
        String lockPwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        String idCard = DigestUtils.md5DigestAsHex(ls.getBytes());
        teacher.setPassword(lockPwd);
        teacher.setLast_idcard(idCard);
        if(teacherService.forget(teacher)) return Result.success("修改密码成功");
        else return Result.fail("修改密码失败");
    }
    @PostMapping("/menu")
    @ApiOperation("判断角色身份")
    public Result checkrole(@RequestBody String tea_id){
        String str = tea_id.substring(0,tea_id.length()-1);
        Teacher teacher = teacherService.login(str);
        return new Result().setCode(200).setMsg("ok").setData(teacher.getIsroot());
    }
    @GetMapping("/logout")
    @ApiOperation("登出")
    public Result logout(){
        return new Result().setCode(200);
    }
}
